package com.scmt.hmzd.utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utils {
    private static final String TOKEN = "mike";

    /**
     * 验证签名
     *
     * @param timestamp
     * @param nonce
     * @param signature
     * @return
     */
    public static boolean check(String timestamp, String nonce, String signature) {
        //将三个参数排序
        String[] strs = new String[]{TOKEN, timestamp, nonce};
        Arrays.sort(strs);
        //将三个参数拼接成一个字符串进行sha1加密
        String str = strs[0] + strs[1] + strs[2];
        //加密
        String mysig = sha1(str);

        return mysig.equalsIgnoreCase(signature);
    }

    /**
     * 加密
     *
     * @param str
     * @return
     */
    private static String sha1(String str) {
        try {
            //获取一个加密对象
            MessageDigest md = MessageDigest.getInstance("sha1");

            //加密
            byte[] digest = md.digest(str.getBytes());
            char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            StringBuilder sb = new StringBuilder();

            for (byte b : digest) {
                sb.append(chars[(b >> 4) & 15]);
                sb.append(chars[b & 15]);
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解析接收到的消息
     *
     * @param is
     * @return
     */
    public static Map<String, String> parseRequest(InputStream is) {
        //保存获取的消息节点
        Map<String, String> requestMap = new HashMap<>();

        try {
            //创建读取对象
            SAXReader reader = new SAXReader();
            //读取流，获取文档对象
            Document document = reader.read(is);
            //获取根元素
            Element rootElement = document.getRootElement();
            //获取素有元素
            List<Element> elements = rootElement.elements();
            //每个元素
            for (Element e : elements) {
                requestMap.put(e.getName(), e.getStringValue());
            }

            return requestMap;
        } catch (Exception e) {

        }
        return null;
    }



    /**
     * 根据url，发送请求，获取access_token
     * 发送get请求
     *
     * @param url
     * @return
     */
    public static String getUrl(String url) {
        //创建url对象
        try {
            URL urlObj = new URL(url);
            //开链接
            URLConnection urlConnection = urlObj.openConnection();
            InputStream is = urlConnection.getInputStream();
            //读取流
            byte[] b = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();
            while (((len = is.read(b)) != -1)) {
                sb.append(new String(b, 0, len));
            }

            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 发送post请求
     *
     * @param url
     * @param data
     * @return
     */
    public static String post(String url, String data) {
        try {
            URL urlObj = new URL(url);
            //开链接
            URLConnection urlConnection = urlObj.openConnection();
            urlConnection.setDoOutput(true);
            OutputStream os = urlConnection.getOutputStream();
            os.write(data.getBytes());
            os.close();
            //获取返回值
            InputStream is = urlConnection.getInputStream();
            byte[] b = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();
            while ((len = is.read(b)) != -1) {
                sb.append(new String(b, 0, len));
            }
            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
